﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using HorizontalAlignment = NPOI.SS.UserModel.HorizontalAlignment;
using FitnessClubManager.GUI;
using FitnessClubManager.BLL;
using FitnessClubManager.DAL;
namespace FitnessClubManager.GUI
{
    public partial class FormThongKeNuocGiaiKhat : FormBase
    {
        public FormThongKeNuocGiaiKhat()
        {
            InitializeComponent();
            gridThongKe.AllowUserToAddRows = false;
            gridThongKe.AllowUserToDeleteRows = false;
            gridThongKe.AutoGenerateColumns = false;
            gridThongKe.ReadOnly = true;
            //this.WindowState = FormWindowState.Maximized;
            hienThiDanhSachNguoiThu();
        }

        /// <summary>
        /// Hiển thị danh sách người thu tiền
        /// </summary>
        void hienThiDanhSachNguoiThu()
        {
            var ds = BizNguoiSuDung.GetListNguoiSuDung();
            ds.Insert(0, new NguoiSuDung() { HoTen = "Tất cả", TenDangNhap = "-1000" });
            bsNguoiThu.DataSource = ds;
            cmbNguoiThu.DataSource = bsNguoiThu;
            cmbNguoiThu.DisplayMember = "HoTen";
            cmbNguoiThu.ValueMember = "TenDangNhap";
        }

        private void bntThongKe_Click(object sender, EventArgs e)
        {
            ColThanhTien.DataPropertyName = "ThanhTien";
            colLai.DataPropertyName = "Lai";
            colThaoTac.Visible = true;
            thongKeChiTiet();
        }

        /// <summary>
        /// Xử lý việc thống kê trên cửa sổ
        /// </summary>
        private void thongKeChiTiet()
        {
            #region Tiền xử lý
            bool ngayLoi = dateFrom.Value.Date > dateTo.Value.Date ? true : false;

            if (ngayLoi)
            {
                MessageBox.Show("Thời gian nhập vào không hợp lệ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            var dFrom = dateFrom.Value;
            var dTo = dateTo.Value;
            var nguoiThu = (NguoiSuDung)cmbNguoiThu.SelectedItem;
            List<NuocGKForGrid> danhSach = new List<NuocGKForGrid>();
            #endregion

            FormProgress frm = new FormProgress();
            frm.DoWork += delegate(object oSender, DoWorkEventArgs oE)
            {
                danhSach = BizNuocGiaiKhat.ThongKeChiTiet(dFrom, dTo, nguoiThu.TenDangNhap == "-1000" ? null : nguoiThu.TenDangNhap);
            };
            frm.RunWorkerCompleted += delegate(object oSender, RunWorkerCompletedEventArgs oE)
            {
                if (oE.Error != null)
                {
                    string msg = oE.Error.Message;
                    if (oE.Error.InnerException != null)
                        msg += oE.Error.InnerException.Message;
                    DialogUtils.ShowError(msg);
                }
                if (danhSach.Count > 0)
                {
                    bsThongKe.DataSource = danhSach;
                    gridThongKe.DataSource = bsThongKe;
                    lbTongTien.Text = (from e in danhSach
                                       select e.SoLuong * e.DonGia).Sum().ToString().FormatCurrency() + " đồng";
                    lbTongLai.Text = (from e in danhSach
                                      select e.SoLuong * (e.DonGia - e.GiaNhap)).Sum().ToString().FormatCurrency() + " đồng";
                }
                else
                {
                    lbTongLai.Text = lbTongTien.Text = "0 đồng";
                    bsThongKe.DataSource = null;
                    gridThongKe.DataSource = bsThongKe;
                }
            };
            frm.StartWorking(this);

        }

        private void dateFrom_ValueChanged(object sender, EventArgs e)
        {
            dateTo.Value = dateFrom.Value;
        }

        private void btnThongKeTongHop_Click(object sender, EventArgs e)
        {
            ColThanhTien.DataPropertyName = "ThanhTienTongHopStr";
            colLai.DataPropertyName = "LaiTongHopStr";
            colThaoTac.Visible = false;
            thongKeTongHop();
        }
        private void thongKeTongHop()
        {
            #region Tiền xử lý
            bool ngayLoi = dateFrom.Value.Date > dateTo.Value.Date ? true : false;

            if (ngayLoi)
            {
                MessageBox.Show("Thời gian nhập vào không hợp lệ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            var dFrom = dateFrom.Value;
            var dTo = dateTo.Value;
            var nguoiThu = (NguoiSuDung)cmbNguoiThu.SelectedItem;
            List<NuocGKForGrid> danhSach = new List<NuocGKForGrid>();
            #endregion

            FormProgress frm = new FormProgress();
            frm.DoWork += delegate(object oSender, DoWorkEventArgs oE)
            {
                danhSach = BizNuocGiaiKhat.ThongKeTongHop(dFrom, dTo, nguoiThu.HoTen, nguoiThu.TenDangNhap == "-1000" ? null : nguoiThu.TenDangNhap);
            };
            frm.RunWorkerCompleted += delegate(object oSender, RunWorkerCompletedEventArgs oE)
            {
                if (oE.Error != null)
                {
                    string msg = oE.Error.Message;
                    if (oE.Error.InnerException != null)
                        msg += oE.Error.InnerException.Message;
                    DialogUtils.ShowError(msg);
                }
                if (danhSach.Count > 0)
                {
                    bsThongKe.DataSource = danhSach;
                    gridThongKe.DataSource = bsThongKe;
                    lbTongTien.Text = (from e in danhSach
                                       select e.ThanhTienTongHop).Sum().ToString().FormatCurrency() + " đồng";
                    lbTongLai.Text = (from e in danhSach
                                      select e.LaiTongHop).Sum().ToString().FormatCurrency() + " đồng";
                }
                else
                {
                    lbTongLai.Text = lbTongTien.Text = "0 đồng";
                    bsThongKe.DataSource = null;
                    gridThongKe.DataSource = bsThongKe;
                }
            };
            frm.StartWorking(this);

        }

        private void gridThongKe_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                if (e.ColumnIndex == 8)
                {
                    NuocGKForGrid dongNuoc = bsThongKe.Current as NuocGKForGrid;
                    if (BizDongNuocGK.Delete(dongNuoc.MaNuocGK, dongNuoc.NgayBan.Value, dongNuoc.TenDangNhap))
                        bsThongKe.RemoveCurrent();
                }
            }
        }
    }
}
